﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using WgFrame.Net;

public class BuZhenPanel : BasePopupPanel
{
    private GList lsZhenFa;

    public override void Init()
    {
        base.Init();
        frame = view.GetChild("n2").asCom;

        lsZhenFa = frame.GetChild("n4").asList;

        for (int i = 0; i<6; i++)
        {
            GButton btn = lsZhenFa.AddItemFromPool().asButton;
            if (i < 3)
            {
                btn.title = "后"+(i+1).ToString();
            }
            else
            {
                btn.title = "前" + (i - 2).ToString();
            }
            btn.draggable = false;
            btn.onDragStart.Add(OnDragStrat);
            btn.onDrop.Add(OnDrop);
        }
    }

    public override void Show(params object[] para)
    {
        MsgListenerMgr.AddMsgListener(MsgListenerType.BuZhen, OnBuZhenChangeMsg);

        this.SetInfo();

        base.Show(para);
    }

    public override void Close()
    {
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.BuZhen, OnBuZhenChangeMsg);
        base.Close();
    }

    public override void Dispose()
    {
        base.Dispose();
    }

    protected override GComponent CreateView()
    {
        return UIPackage.CreateObject("Main", "panel_zheng1").asCom;
    }

    private void SetInfo()
    {
        for (int i=0; i<lsZhenFa.numChildren; i++)
        {
            GButton button = lsZhenFa.GetChildAt(i).asButton;
            if (i < 3)
            {
                button.title = "后" + (i + 1).ToString();
            }
            else
            {
                button.title = "前" + (i - 2).ToString();
            }
            button.draggable = false;
            button.color = CommonUtil.Cyan;
            button.data = null;
        }

        List<Hero> heros = GameNetwork.Player.FightHeroMgr.GetAll();
        foreach (Hero hero in heros)
        {
            if (hero.bzPos < 1 || hero.bzPos > 6)
            {
                continue;
            }
            int pos = hero.bzPos;
            if (pos <= 3)
            {
                pos += 3;
            }
            else
            {
                pos -= 3;
            }

            GButton button = lsZhenFa.GetChildAt(pos - 1).asButton;
            button.title = hero.GetName();
            button.data = hero.id;
            button.color = CommonUtil.White;
            button.draggable = true;
        }
    }

    private byte GetServerPos(int idx)
    {
        if (idx < 3)
        {
            return (byte) (idx + 4);
        }
        else
        {
            return (byte)(idx - 2);
        }
    }

    private void OnDrop(EventContext context)
    {
        GButton btnSource = context.initiator as GButton;
        GButton btnTag = context.sender as GButton;

        RequestHelper.SendBuZhenRequest((long)btnSource.data, GetServerPos(lsZhenFa.GetChildIndex(btnTag)));
    }

    private void OnDragStrat(EventContext context)
    {
        GButton btn = context.sender as GButton;

        context.PreventDefault();

        DragDropManagerEx.inst.StartDrag(btn, btn.title, btn.title);
    }

    private void OnBuZhenChangeMsg(params object[] para)
    {
        SetInfo();
    }
}
